package com.cts.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cts.annotation.OperationLogger;
import com.cts.entity.Category;
import com.cts.entity.Friend;
import com.cts.entity.Tag;
import com.cts.model.dto.FriendDTO;
import com.cts.model.vo.PageResultVO;
import com.cts.model.vo.Result;
import com.cts.service.FriendService;
import com.cts.util.DateFormatUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @projectName: CBlog
 * @package: com.cts.controller.admin
 * @className: FriendAdminController
 * @author: chentongsheng
 * @description: TODO
 * @date: 2023/3/18 09:31
 * @version: 1.0
 */
@RestController
@RequestMapping("/admin")
public class FriendAdminController {

    @Autowired
    FriendService friendService;

    @Autowired
    DateFormatUtils dateFormatUtils;

    @OperationLogger("友链管理列表查询")
    @ApiOperation("友链管理列表查询")
    @PostMapping("/friends")
    public Result getFriendList(@RequestBody FriendDTO friendDTO) {
        PageResultVO<Friend> pageResult = friendService.selectFriendList(friendDTO);
        return Result.ok("请求成功",pageResult);

    }

    @OperationLogger("友链管理单笔查询")
    @ApiOperation("友链管理单笔查询")
    @RequestMapping("/friend")
    public Result getFriendById(@RequestParam Integer id) {
        Friend friend = friendService.getById(id);
        return Result.ok("请求成功", friend);
    }

    @OperationLogger("友链管理新增")
    @ApiOperation("友链管理新增")
    @PostMapping("/friend")
    public Result addFriend(@RequestBody Friend friend) {
        friend.setCreateTime(dateFormatUtils.getNow());
        friend.setViews(0);
        Boolean save = friendService.save(friend);
        if(save) {
            return Result.ok("新增成功");
        }
        return Result.error("新增失败");
    }

    @OperationLogger("友链管理更新")
    @ApiOperation("友链管理更新")
    @PostMapping("/updateFriend")
    public Result updateFriend(@RequestBody Friend friend) {
        Boolean save = friendService.updateById(friend);
        if(save) {
            return Result.ok("更新成功");
        }
        return Result.error("更新失败");
    }

    @OperationLogger("友链管理删除")
    @ApiOperation("友链管理删除")
    @PostMapping("/removeFriend")
    public Result removeFriend(@RequestParam Integer id) {
        Boolean remove = friendService.removeById(id);
        if(remove) {
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }

    @OperationLogger("友链管理发布")
    @ApiOperation("友链管理发布")
    @PostMapping("/publishFriend")
    public Result publishFriend(@RequestParam Integer id, Boolean isPublished) {
        Boolean published = friendService.publishFriend(id, isPublished);
        if(published) {
            return Result.ok("发布成功");
        }
        return Result.error("发布失败");
    }

    @OperationLogger("友链管理搜索")
    @ApiOperation("友链管理搜索")
    @PostMapping("/getFriendByName")
    public Result getFriendByName(@RequestParam String nickName) {
        List<Friend> list = friendService.getFriendByName(nickName);
        return Result.ok("请求成功", list);
    }
}
